package com.hrp.controller;

import com.hrp.domain.TestExportExcel;
import com.hrp.hrpdao.TestExportExcelMapper;
import com.hrp.service.ExcelExportService;
import com.hrp.service.TransportRecordQueryService;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2019/5/3 0003.
 */
@RestController
@RequestMapping(value="/export",method= RequestMethod.GET)
public class ExcelExportController {

    @Autowired
    TestExportExcelMapper mapper;


    @Autowired
    private TransportRecordQueryService transportRecordQueryService;


    @Autowired
    public ExcelExportService excelExportService;


    @RequestMapping(value = "/export1", method = RequestMethod.GET)
    public void export(HttpServletResponse response, Integer page, Integer rows, @RequestParam(name = "sort", defaultValue = "id") String sort,
                       @RequestParam(name = "order", defaultValue = "asc") String order,
                       @RequestParam(name = "sendTimeKsrq", defaultValue = "2018-01-01") String sendTimeKsrq,
                       @RequestParam(name = "sendTimeJsrq", defaultValue = "2018-01-01") String sendTimeJsrq,
                       @RequestParam(name = "varietiesIdStr", defaultValue = "-1") String varietiesIdStr,
                       @RequestParam(name = "supplierIdStr", defaultValue = "-1") String supplierIdStr,
                       @RequestParam(name = "receiverIdStr", defaultValue = "-1") String receiverIdStr) throws IOException {


        HSSFWorkbook workbook = excelExportService.getTransportRecordByPage(page, rows, sort, order, sendTimeKsrq, sendTimeJsrq, varietiesIdStr, supplierIdStr, receiverIdStr);
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //System.out.println("当前时间：" + sdf.format(d));
        String fileName = sdf.format(d) + ".xls";
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
        workbook.close();

//        List<TestExportExcel> queryAll = mapper.queryAll();
//        //工作簿
//        HSSFWorkbook workbook = new HSSFWorkbook();
//        HSSFSheet sheet = workbook.createSheet("测试表");
//        //设置导出的表的名字
//        String fileName = "ceshi.xls";
//        //设置头
//        int rowNum =1;
//        String [] headers = {"学号","姓名","创建时间"};
//        HSSFRow row = sheet.createRow(0);
//        //设置为居中加粗
//        HSSFCellStyle style = workbook.createCellStyle();
//        style.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
//        HSSFFont font = workbook.createFont();
//        font.setBold(true);
//        style.setAlignment(HorizontalAlignment.CENTER);
//        style.setFont(font);
//
//        for(int i=0;i<headers.length;i++) {
//            HSSFCell cell = row.createCell(i);
//            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
//            cell.setCellValue(text);
//            cell.setCellStyle(style);
//        }
//
//        for (TestExportExcel testExportExcel : queryAll) {
//            HSSFRow rown = sheet.createRow(rowNum);
//            rown.createCell(0).setCellValue(testExportExcel.getId());
//            rown.createCell(1).setCellValue(testExportExcel.getName());
//            HSSFCell createCell = rown.createCell(2);
//            createCell.setCellValue(testExportExcel.getCreatTime());
//            createCell.setCellStyle(style);
//            rowNum++;
//        }
//        response.setContentType("application/octet-stream");
//        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
//        response.flushBuffer();
//        workbook.write(response.getOutputStream());
//        workbook.close();

    }


    //汇总导出
    @RequestMapping(value = "/exportSummary", method = RequestMethod.GET)
    public void exportSummary(HttpServletResponse response, @RequestParam(name = "sort", defaultValue = "supplierId") String sort,
                              @RequestParam(name = "order", defaultValue = "asc") String order,
                              @RequestParam(name = "sendTimeKsrq", defaultValue = "2018-01-01") String sendTimeKsrq,
                              @RequestParam(name = "sendTimeJsrq", defaultValue = "2018-01-01") String sendTimeJsrq,
                              @RequestParam(name = "varietiesIdStr", defaultValue = "-1") String varietiesIdStr,
                              @RequestParam(name = "supplierIdStr", defaultValue = "-1") String supplierIdStr,
                              @RequestParam(name = "receiverIdStr", defaultValue = "-1") String receiverIdStr) throws IOException {


        HSSFWorkbook workbook = excelExportService.getTransportRecordexportSummary(sort, order, sendTimeKsrq, sendTimeJsrq, varietiesIdStr, supplierIdStr, receiverIdStr);
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //System.out.println("当前时间：" + sdf.format(d));
        String fileName = sdf.format(d) + ".xls";
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
        workbook.close();

    }


    //应收账款导出
    @RequestMapping(value = "/exportAccountsReceivable", method = RequestMethod.GET)
    public void exportAccountsReceivable(HttpServletResponse response,
                                         @RequestParam(name = "page", defaultValue = "1") Integer page,
                                         @RequestParam(name = "rows", defaultValue = "300") Integer rows,
                                         @RequestParam(name = "startTime", defaultValue = "2018-01-01") String startTime,
                                         @RequestParam(name = "endTime", defaultValue = "2018-01-01") String endTime,
                                         @RequestParam(name = "receiverIdStr", defaultValue = "-1") String receiverIdStr
                              ) throws IOException {


        HSSFWorkbook workbook = excelExportService.exportAccountsReceivable(page, rows, startTime, endTime,receiverIdStr);
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //System.out.println("当前时间：" + sdf.format(d));
        String fileName = sdf.format(d) + ".xls";
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
        workbook.close();

    }

    @RequestMapping(value = "/excelExportForManySheet", method = RequestMethod.GET)
    public void excelExportForManySheet(HttpServletResponse response, Integer page, Integer rows, @RequestParam(name = "sort", defaultValue = "id") String sort,
                       @RequestParam(name = "order", defaultValue = "asc") String order,
                       @RequestParam(name = "sendTimeKsrq", defaultValue = "2018-01-01") String sendTimeKsrq,
                       @RequestParam(name = "sendTimeJsrq", defaultValue = "2018-01-01") String sendTimeJsrq,
                       @RequestParam(name = "varietiesIdStr", defaultValue = "-1") String varietiesIdStr,
                       @RequestParam(name = "supplierIdStr", defaultValue = "-1") String supplierIdStr,
                       @RequestParam(name = "receiverIdStr", defaultValue = "-1") String receiverIdStr) throws IOException {


        HSSFWorkbook workbook = excelExportService.excelExportForManySheet(page, rows, sort, order, sendTimeKsrq, sendTimeJsrq, varietiesIdStr, supplierIdStr, receiverIdStr);
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //System.out.println("当前时间：" + sdf.format(d));
        String fileName = sdf.format(d) + ".xls";
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
        workbook.close();

    }

}
